package com.purchase.ml.exception;


import com.purchase.ml.bean.base.ApiRestResult;
import com.purchase.ml.enums.RetStatusNum;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.CredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.NoHandlerFoundException;

import javax.servlet.http.HttpServletRequest;

/**
 * @description: 全局异常处理
 * @author kyyang
 * @date 2021/1/8 9:33
 * @version 1.0
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

	/**
	 * 处理自定义的业务异常
	 * @param req
	 * @param e
	 * @return
	 */
    @ExceptionHandler(value = BizException.class)
    @ResponseBody
	public ApiRestResult bizExceptionHandler(HttpServletRequest req, BizException e){
    	log.error("发生业务异常！原因是：{}",e.getMessage());

    	return ApiRestResult.err(e.getErrorMsg(),e.getErrorCode());
    }

	/**
	 * 处理空指针的异常
	 * @param req
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value =NullPointerException.class)
	@ResponseBody
	public ApiRestResult exceptionHandler(HttpServletRequest req, NullPointerException e){
		log.error("发生空指针异常！原因是:",e);
		return ApiRestResult.err();
	}


    /**
        * 处理其他异常
     * @param req
     * @param e
     * @return
     */
    @ExceptionHandler(value =Exception.class)
	@ResponseBody
	public ApiRestResult exceptionHandler(HttpServletRequest req, Exception e){
		log.error("其它异常！原因是:",e);
		if (e instanceof NoHandlerFoundException) {
			return ApiRestResult.err(RetStatusNum.NO_RESOURCE.getMessage(),RetStatusNum.NO_RESOURCE.getCode());
		}if(e instanceof MethodArgumentNotValidException){
			return ApiRestResult.err(((MethodArgumentNotValidException) e).getBindingResult().getFieldError().getDefaultMessage(),RetStatusNum.PARAMS_ERROR.getCode());
		}if(e instanceof UnauthorizedException){
			return ApiRestResult.err(RetStatusNum.NO_PERMISSION.getMessage(),RetStatusNum.NO_PERMISSION.getCode());
		}if(e instanceof HttpMessageNotReadableException){
			return ApiRestResult.err(e.getMessage(),RetStatusNum.PARAMS_ERROR.getCode());
		}if(e instanceof CredentialsException){
			return ApiRestResult.err(RetStatusNum.ACC_NO.getMessage(),RetStatusNum.ACC_NO.getCode());
		}if(e instanceof LockedAccountException){
			return ApiRestResult.err(RetStatusNum.ACC_FREEZE.getMessage(),RetStatusNum.ACC_FREEZE.getCode());
		}if(e instanceof BindException){
			return ApiRestResult.err(((BindException) e).getBindingResult().getFieldError().getDefaultMessage(),RetStatusNum.PARAMS_ERROR.getCode());
		}if(e instanceof org.springframework.dao.DuplicateKeyException){
			return ApiRestResult.err(RetStatusNum.SQLDUPLICATEKEY_ERROR.getMessage(),RetStatusNum.SQLDUPLICATEKEY_ERROR.getCode());		}
		else{
			return ApiRestResult.err();
		}
    }

}

